package com.jxs.android.weixi;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.weiget.BuLoadingDailog;
import com.telecom.weixi.R;

public abstract class AbstractListActivity extends AbstractActivity {

	enum Which {
		MY_CUSTOMER_USER, //
		MY_CUSTOMER_USER_LOST, //
		MY_CUSTOMER_CUSTOMER, //
		MY_ACHIVERMENT, //
		MY_WORK_USER_DETAIL, //
		MY_WORK_VIP, //
		MY_WORK_CUSTOMER, //
		NOTICE_PAGE, //
		USER_SEARCH_PHONE, //
		USER_SEARCH_NAME, //
		TERM_SEACHER_PHONE, //
		OWE_FEE_SEARCH_PHONE, //
		OWE_FEE_SEARCH_NAME; // /
	}

	/**
	 * @Des 用于启动对应的应用程序
	 * @param showDialog
	 *            TODO
	 * @param context
	 * @param targetActivity
	 */
	protected void getWebServiceData(final String ticket, final Which which, final boolean showDialog, final DataInitListeren dataInitListeren, final Object... params) {

		final BuLoadingDailog buLoadingDailog = new BuLoadingDailog(this);
		buLoadingDailog.setCancelable(true);
		buLoadingDailog.showLoading(R.string.prompt_loading);
		if (!showDialog) {
			buLoadingDailog.cancel();
		}
		new Thread() {
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				getListData(ticket, which, params);
				dataInitListeren.initData(buLoadingDailog, showDialog);
			}
		}.start();
	}

	static public interface DataInitListeren {
		public void initData(BuLoadingDailog progressDialog, boolean isShow);
	}

	public abstract String getMethod();

	protected void getListData(String ticket, Which which, final Object... params) {
		String wsdl_url = WeixiApplication.WSDL_URL;// wsdl
		String namespace = WeixiApplication.WSDL_NAME_SPACE;// namespace
		String methodName = getMethod();// 要调用的方法名称
		SoapObject request = new SoapObject(namespace, methodName);
		request.addProperty("ticket", ticket);
		if (which.ordinal() == Which.MY_CUSTOMER_USER.ordinal()) {

			request.addProperty("conditionProduct", params[0].toString());
			request.addProperty("conditionHighValue", params[1].toString());
			request.addProperty("conditionVipClub", params[2].toString());
			request.addProperty("conditionAddStock", params[3].toString());
			request.addProperty("conditionAccNbr", params[4].toString());
			request.addProperty("orderFlag", params[5].toString());
			request.addProperty("pageSize", Integer.valueOf(params[6].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[7].toString()));

		} else if (which.ordinal() == Which.MY_CUSTOMER_USER_LOST.ordinal()) {
			request.addProperty("conditionProduct", params[0].toString());
			request.addProperty("conditionHighValue", params[1].toString());
			request.addProperty("conditionVipClub", params[2].toString());
			request.addProperty("conditionAccNbr", params[3].toString());
			request.addProperty("orderFlag", params[4].toString());
			request.addProperty("pageSize", Integer.valueOf(params[5].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[6].toString()));

		} else if (which.ordinal() == Which.MY_CUSTOMER_CUSTOMER.ordinal()) {
			request.addProperty("conditionCustName", params[0].toString());
			request.addProperty("pageSize", Integer.valueOf(params[1].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[2].toString()));
		} else if (which.ordinal() == Which.MY_ACHIVERMENT.ordinal()) {
			request.addProperty("pageSize", Integer.valueOf(params[0].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[1].toString()));
		} else if (which.ordinal() == Which.MY_WORK_USER_DETAIL.ordinal()) {
			// getTabId(), conditionProduct, conditionHighValue,
			// conditionVipClub, conditionAddStock, conditionAccNbr,
			// conditionSortFlag, conditionSortFlag,
			request.addProperty("tabType", params[0].toString());
			request.addProperty("conditionProduct", params[1].toString());
			request.addProperty("conditionHighValue", params[2].toString());
			request.addProperty("conditionVipClub", params[3].toString());
			request.addProperty("conditionAddStock", params[4].toString());
			request.addProperty("conditionAccNbr", params[5].toString());
			request.addProperty("orderFlag", params[6].toString());
			request.addProperty("pageSize", Integer.valueOf(params[7].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[8].toString()));
		} else if (which.ordinal() == Which.MY_WORK_VIP.ordinal()) {
			request.addProperty("tabType", params[0].toString());
			request.addProperty("orderFlag", params[1].toString());
			request.addProperty("pageSize", Integer.valueOf(params[2].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[3].toString()));
		} else if (which.ordinal() == Which.MY_WORK_CUSTOMER.ordinal()) {
			request.addProperty("pageSize", Integer.valueOf(params[0].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[1].toString()));
		} else if (which.ordinal() == Which.NOTICE_PAGE.ordinal()) {
			request.addProperty("pageSize", Integer.valueOf(params[0].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[1].toString()));
		} else if (which.ordinal() == Which.USER_SEARCH_PHONE.ordinal()) {
			request.addProperty("acc_nbr", params[0].toString());
			request.addProperty("pageSize", Integer.valueOf(params[1].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[2].toString()));
		} else if (which.ordinal() == Which.USER_SEARCH_NAME.ordinal()) {
			request.addProperty("serv_name", params[0].toString());
			request.addProperty("pageSize", Integer.valueOf(params[1].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[2].toString()));
		} else if (which.ordinal() == Which.OWE_FEE_SEARCH_NAME.ordinal()) {
			request.addProperty("serv_name", params[0].toString());
			request.addProperty("pageSize", Integer.valueOf(params[1].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[2].toString()));
		} else if (which.ordinal() == Which.OWE_FEE_SEARCH_PHONE.ordinal()) {
			request.addProperty("acc_nbr", params[0].toString());
			request.addProperty("pageSize", Integer.valueOf(params[1].toString()));
			request.addProperty("pageNo", Integer.valueOf(params[2].toString()));
		}
		BuWebService webService = new BuWebService(wsdl_url, request);
		webService.setDoNet(false);
		webService.requestWSDL(new IWebService() {
			@Override
			public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
				SoapObject response = (SoapObject) envelope.getResponse();
				paser(response, params);
			}

			@Override
			public void error(String message, Exception e) {
				// errorMsgPop(message);
			}
		});
	}

	protected abstract void paser(SoapObject response, final Object... params);
}
